package net.orange.framework.model;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import net.orange.architecture.model.BaseValueObject;
import net.orange.architecture.validator.StringNotNullValidator;
import net.orange.architecture.validator.Validator;
import net.orange.framework.validator.CommoninfoDuplicateValidator;

/**
 * The common information, which include the type & code. it can be define as
 * the system simple master data, just like the gender, week. All the type is
 * defined in the object CommoninfoType.
 */
@Entity
@Table(name = "t_commoninfo")
public class Commoninfo extends BaseValueObject {

	private static final long serialVersionUID = 4717543872135218880L;

	/**
	 * The common information type.
	 */
	@Column(name = "type")
	private String type;

	/**
	 * The common information code.
	 */
	@Column(name = "code")
	private String code;

	/**
	 * The common information value.
	 */
	@Column(name = "value")
	private String value;

	/**
	 * The sequence when it is the queue.
	 */
	@Column(name = "seq")
	private int seq;

	/**
	 * @return Returns the _typename.
	 */
	public String getTypename() {
		return (type != null) ? CommoninfoType.parseOf(type).getDesc() : null;
	}

	/**
	 * @return Returns the code.
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @return Returns the seq.
	 */
	public int getSeq() {
		return seq;
	}

	/**
	 * @return Returns the type.
	 */
	public String getType() {
		return type;
	}

	/**
	 * @return Returns the value.
	 */
	public String getValue() {
		return value;
	}

	/**
	 * @param code
	 *            The code to set.
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @param seq
	 *            The seq to set.
	 */
	public void setSeq(int seq) {
		this.seq = seq;
	}

	/**
	 * @param type
	 *            The type to set.
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @param value
	 *            The value to set.
	 */
	public void setValue(String value) {
		this.value = value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.model.BaseValueObject#satisfy(java.util.Collection)
	 */
	public boolean satisfy(Collection<String> msg) throws Exception {
		if (msg == null)
			msg = new ArrayList<String>();

		Validator validator = new Validator(msg);

		validator.add(new StringNotNullValidator(getType(), "type"));
		validator.add(new StringNotNullValidator(getCode(), "code"));
		validator.add(new StringNotNullValidator(getValue(), "value"));
		validator.add(new CommoninfoDuplicateValidator(this));

		validator.validate();

		return (msg == null || msg.size() <= 0);
	}
}
